// https://juejin.im/post/5ead36f75188256d7674a024
const SinaUrl = "https://s.weibo.com/top/summary?cate=realtimehot";
const weiboURL = "https://s.weibo.com"
const cheerio = require('cheerio');
const superagent = require('superagent');
const fs = require('fs');
const {
    scheduleJob
} = require('node-schedule');
const rule = "* * * * * *";
scheduleJob(rule, () => {
    // console.log(new Date());
    getData();
});

function getData(){
    superagent.get(SinaUrl, (err, res) => {
        // console.log(err, res);
        if(err){
            console.log('get error')
            return;
        }
        const $ = cheerio.load(res.text);
        let hotList = [];
        $("#pl_top_realtimehot table tbody tr").each(function (index) {
            if (index !== 0) {
                const $td = $(this).children().eq(1);
                const link = weiboURL + $td.find("a").attr("href");
                const text = $td.find("a").text();
                const hotValue = $td.find("span").text();
                const icon = $td.find("img").attr("src") ?
                    "https:" + $td.find("img").attr("src") :
                    "";
                hotList.push({
                    index,
                    link,
                    text,
                    hotValue,
                    icon,
                });
            }
        });
        // console.log(hotList);
        fs.writeFileSync(
            `${__dirname}/hotSearch.json`,
            JSON.stringify(hotList),
            "utf-8"
        );
        console.log('set success')
    })
}